    <?php

App::uses('AuthComponent', 'Controller/Component');

class User extends AppModel
{
    public $name= 'User';
    
    public $hasMany = array(
        'Organization' => array(
            'className'  => 'Organization',
            'foreignKey'   => 'creator_id',
            'order'      => 'Organization.created DESC'
        )

    );
    
    
    var $validate = array(
        
       'username' => array( 'required'=> array(
                                'rule' => array('notEmpty'),
                                'message' => 'A username is required'
                            )
                    ),
        
        'password' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'A password is required'
            )
        ),
        'email' => array(
            'required' => array(
                'rule' => array('email',true),
                'message' => 'A valid email is required'
            )
        ),
        'role' => array(
            'valid' => array(
                'rule' => array('inList', array('admin', 'author')),
                'message' => 'Please enter a valid role',
                'allowEmpty' => false
            )
        ),
        'gravatar' => array(
            'valid' => array(
                'rule' => array('notEmpty'),
                'message' => 'Please enter a valid gravatar',
                'allowEmpty' => false
            )
        )
            
   );
    public function beforeValidate($options = array()){
        if (isset($this->data[$this->alias]['email'])){
            $this->data[$this->alias]['gravatar']= md5(strtolower(trim($this->data[$this->alias]['email'])));
        }
    }
    public function beforeSave($options = array()) {
        if (isset($this->data[$this->alias]['password'])) {
            $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
        }
        return true;
    }
    
}
?>
